package com.maoyu.cloud.user.service.impl;

import com.maoyu.cloud.login.entity.LoginUserEntity;
import com.maoyu.cloud.login.entity.MenuLevelEntity;
import com.maoyu.cloud.mybatis.entity.PtMenuEntity;
import com.maoyu.cloud.mybatis.entity.PtUserEntity;
import com.maoyu.cloud.user.repository.LoginDao;
import com.maoyu.cloud.user.service.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("loginService")
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginDao loginDao;
    @Override
    public PtUserEntity queryUserInfoByName(String username) {
        return loginDao.queryUserInfoByName(username);
    }

    private Logger logger = LogManager.getLogger(this.getClass());

    @Override
    public List<PtMenuEntity> queryMenuInfoListByUuid() {
        LoginUserEntity userEntity = (LoginUserEntity)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String lang = LocaleContextHolder.getLocale().toString();
        return loginDao.queryMenuInfoListByUuidLang(userEntity.getUserUuid(), lang);
    }

    @Override
    public List<MenuLevelEntity> queryMenuLevelList() {
        LoginUserEntity userDetails = (LoginUserEntity)
                SecurityContextHolder.getContext()
                        .getAuthentication()
                        .getPrincipal();
        String userUUID = userDetails.getUserUuid();
        String langId = LocaleContextHolder.getLocale().toString();
        List<PtMenuEntity> ptMenuEntityList =
                loginDao.queryMenuListByUserId(userUUID,langId);
        MenuLevelEntity menuLevel1;
        MenuLevelEntity menuLevel2;
        MenuLevelEntity menuLevel3;
        List<MenuLevelEntity> menuLevelList = new ArrayList<>();
        //level 1
        for(PtMenuEntity menu1:ptMenuEntityList){
            if(menu1.getMenuLevel() == 1){
                menuLevel1 = new MenuLevelEntity(menu1);
                menuLevelList.add(menuLevel1);
                //level 2
                for(PtMenuEntity menu2:ptMenuEntityList){
                    if(menu1.getMenuId() != null &&
                            menu1.getMenuId().equals(menu2.getParentId())){
                        menuLevel2 = new MenuLevelEntity(menu2);
                        menuLevel1.getChildrenList().add(menuLevel2);
                        //level 3
                        for(PtMenuEntity menu3:ptMenuEntityList){
                            if(menu2.getMenuId() != null &&
                                    menu2.getMenuId().equals(menu3.getParentId())) {
                                menuLevel3 = new MenuLevelEntity(menu3);
                                menuLevel2.getChildrenList().add(menuLevel3);
                            }
                        }
                    }
                }
            }
        }
        logger.info(menuLevelList);
            return menuLevelList;
    }

    /**
     * 查询用户权限
     * @return 权限列表
     */
    @Override
    public List<String> queryUserPermission(String userUuid) {
        return loginDao.queryUserPermission(userUuid);
    }

}
